<?php

/**
 * creditMemo actions.
 *
 * @package    erp-crm
 * @subpackage creditMemo
 * @author     Milwell H. Sia
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class creditMemoActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request){}
  
  public function executeCreate(sfWebRequest $request){
  
    $invoice_iden = $request->getParameter('invoice_iden');
    $this->invoice = InvoicePeer::retrieveByPk($invoice_iden);
    $this->forward404Unless($this->invoice);
    $customer_iden = $this->invoice->getCustomerIden();
    $this->accountCustomer = AccountCustomerPeer::retrieveByPk($customer_iden);
    $this->forward404Unless($this->accountCustomer);
    $this->documentForm = new DocumentForm();
    $this->documentForm->setDefault('owner_iden',$this->invoice->getDocument()->getOwnerIden());
    $this->documentForm->setOwnerReadOnly();
    $this->creditMemoForm = new CreditMemoForm();
    $this->creditMemoForm->setDefault('customer_iden',$customer_iden);
    $this->creditMemoForm->setDefault('invoice_iden',$invoice_iden);
    $this->contact = $this->accountCustomer->getContact();
    $this->error_message = '';
    if(sfConfig::get('mod_creditmemo_number_auto'))
      $this->creditMemoForm->setNumberReadOnly();
    $this->setLayout(false);
  }
  
  public function executeInsert(sfWebRequest $request){
  
    $document_params = $request->getParameter('document');
    $credit_memo_params = $request->getParameter('credit_memo');
    
    $document_params['document_type']='DEBIT MEMO';
    $document_params['void']='N';
    $document_params['void_date']=null;
    
    $document_params['document_iden'] = myStringGenerator::getIden();
    $credit_memo_params['credit_iden'] = $document_params['document_iden'];
    
    $this->documentForm = new DocumentForm();
    $this->creditMemoForm = new CreditMemoForm();
    
    if((sfConfig::get('mod_creditmemo_number_auto'))&&(! $credit_memo_params['credit_number'])){
      $credit_memo_number = ListDocumentTypesPeer::getNextId('CREDIT',$credit_memo_params['credit_type']);
      if ($credit_memo_number){
        $credit_memo_params['credit_number'] = vsprintf(sfConfig::get('mod_creditmemo_number_format'),$credit_memo_number);
        $this->creditMemoForm->setNumberTypeReadOnly();
      }
    }
    
    $this->documentForm->bind($document_params);
    $this->creditMemoForm->bind($credit_memo_params);
    
    $this->error_message = '';
    if((sfConfig::get('mod_creditmemo_number_strict'))&&($this->isUnique(null,$credit_memo_params['credit_type'],$credit_memo_params['credit_number'])>0)){
      $this->invoice = InvoicePeer::retrieveByPk($credit_memo_params['invoice_iden']);
      $this->accountCustomer = AccountCustomerPeer::retrieveByPk($credit_memo_params['customer_iden']);
      $this->forward404Unless($this->accountCustomer);
      $this->contact = $this->accountCustomer->getContact();
      $this->setLayout(false);
      $this->error_message = $credit_memo_params['credit_type'].'-'.$credit_memo_params['credit_number'].' already exists.';
      return $this->setTemplate('create');
    }
    if(!($this->documentForm->isValid() && $this->creditMemoForm->isValid())){
      $this->invoice = InvoicePeer::retrieveByPk($credit_memo_params['invoice_iden']);
      $this->accountCustomer = AccountCustomerPeer::retrieveByPk($credit_memo_params['customer_iden']);
      $this->forward404Unless($this->accountCustomer);
      $this->contact = $this->accountCustomer->getContact();
      $this->setLayout(false);
      return $this->setTemplate('create');
    }

    $con = Propel::getConnection(LogTransactionPeer::DATABASE_NAME,Propel::CONNECTION_WRITE);
    $con->beginTransaction();
    try{   
      $this->documentForm->save();
      $this->creditMemoForm->save();
      $log_transaction = new LogTransaction();
      $log_transaction->setTransactionType('CREDIT MEMO');
      $log_transaction->setIden1($this->creditMemoForm->getObject()->getCustomerIden());
      $log_transaction->setType1('CLIENT');
      $log_transaction->setIden2($this->creditMemoForm->getObject()->getCreditIden());
      $log_transaction->setType2('CREDIT MEMO');
      $log_transaction->setIden3($this->creditMemoForm->getObject()->getInvoiceIden());
      $log_transaction->setType3('INVOICE');
      $log_transaction->setDebit($this->creditMemoForm->getObject()->getAmount());
      $log_transaction->setDocumentIden($this->creditMemoForm->getObject()->getCreditIden());
      $log_transaction->save();
      $log_transaction = new LogTransaction();
      $log_transaction->setTransactionType('INVOICE');
      $log_transaction->setIden1($this->creditMemoForm->getObject()->getCustomerIden());
      $log_transaction->setType1('CLIENT');
      $log_transaction->setIden2($this->creditMemoForm->getObject()->getInvoiceIden());
      $log_transaction->setType2('INVOICE');
      $log_transaction->setIden3($this->creditMemoForm->getObject()->getCreditIden());
      $log_transaction->setType3('CREDIT MEMO');
      $log_transaction->setCredit($this->creditMemoForm->getObject()->getAmount());
      $log_transaction->setDocumentIden($this->creditMemoForm->getObject()->getCreditIden());
      $log_transaction->save();
      $con->commit();
      $this->redirect('creditMemo/edit?credit_iden='.$this->creditMemoForm->getObject()->getCreditIden());
    }catch(Exception $e){
      $con->rollBack();
      $request->getParameterHolder()->set('message','P'.$e->getMessage().'Y');
      $this->forward('main','error');
    }
    
  }
  
  public function executeEdit(sfWebRequest $request){
    $creditMemo = CreditMemoPeer::retrieveByPk($request->getParameter('credit_iden'));
    $this->forward404Unless($creditMemo);
    $document = $creditMemo->getDocument();
    $this->documentForm = new DocumentForm($document);
    $this->creditMemoForm = new CreditMemoForm($creditMemo);
    $this->accountCustomer = $creditMemo->getAccountCustomer();
    $this->contact = $this->accountCustomer->getContact();
    $this->invoice = $creditMemo->getInvoice();
    $this->setLayout(false);
    $this->setTemplate('create');
    if(sfConfig::get('mod_creditmemo_number_auto'))
      $this->creditMemoForm->setNumberTypeReadOnly();
    $this->error_message = '';
  }

  public function executeSearch(sfWebRequest $request){
    $this->creditMemoSearchForm = new myCreditMemoSearchForm();
  }
  
  public function executeDoSearch(sfWebRequest $request){
    $search_keys = $request->getParameter('skey_credit');
    $creditMemoSearchForm = new myCreditMemoSearchForm();
    $creditMemoSearchForm->bind($search_keys);
    
    $c = new Criteria();
    if($creditMemoSearchForm->getValue('credit_type'))
      $c->add(CreditMemoPeer::CREDIT_TYPE,$creditMemoSearchForm->getValue('credit_type'));
    if($creditMemoSearchForm->getValue('credit_number_from'))
      $c->add(CreditMemoPeer::CREDIT_NUMBER,$creditMemoSearchForm->getValue('credit_number_from'),Criteria::GREATER_EQUAL);
    if($creditMemoSearchForm->getValue('credit_number_to'))
      $c->add(CreditMemoPeer::CREDIT_NUMBER,$creditMemoSearchForm->getValue('credit_number_to'),Criteria::LESS_EQUAL);
    if($creditMemoSearchForm->getValue('invoice_type'))
      $c->add(InvoicePeer::INVOICE_TYPE,$creditMemoSearchForm->getValue('invoice_type'));
    if($creditMemoSearchForm->getValue('invoice_number_from'))
      $c->add(InvoicePeer::INVOICE_ID,$creditMemoSearchForm->getValue('invoice_number_from'),Criteria::GREATER_EQUAL);
    if($creditMemoSearchForm->getValue('invoice_number_to'))
      $c->add(InvoicePeer::INVOICE_ID,$creditMemoSearchForm->getValue('invoice_number_to'),Criteria::LESS_EQUAL);
    if($creditMemoSearchForm->getValue('credit_date_from'))
      $c->add(DocumentPeer::DOCUMENT_DATE,$creditMemoSearchForm->getValue('credit_date_from'),Criteria::GREATER_EQUAL);
    if($creditMemoSearchForm->getValue('credit_date_to'))
      $c->add(DocumentPeer::DOCUMENT_DATE,$creditMemoSearchForm->getValue('credit_date_to'),Criteria::LESS_EQUAL);
    if($creditMemoSearchForm->getValue('customer_name'))
      $c->add(ContactPeer::LIST_NAME,$creditMemoSearchForm->getValue('customer_name'),Criteria::GREATER_EQUAL);
    if($creditMemoSearchForm->getValue('show_void_only')!='Y')
      $c->add(DocumentPeer::VOID,'N');
    else
      $c->add(DocumentPeer::VOID,'Y');
    $c->addJoin(CreditMemoPeer::CREDIT_IDEN,DocumentPeer::DOCUMENT_IDEN);
    $c->addJoin(CreditMemoPeer::CUSTOMER_IDEN,ContactPeer::IDEN);
    $c->addJoin(CreditMemoPeer::INVOICE_IDEN,InvoicePeer::INVOICE_IDEN);
    $this->creditMemos = CreditMemoPeer::doSelect($c);
  }
  
  
  public function executeVoid(sfWebRequest $request){
    $creditMemo = CreditMemoPeer::retrieveByPk($request->getParameter('credit_iden'));
    $this->forward404Unless($creditMemo);
    $document = $creditMemo->getDocument();
    $document->void();
    return $this->renderText(json_encode(array('credit_iden'=>$creditMemo->getCreditIden(),)));
  }
  
  public function executeGetTd(sfWebRequest $request){
    $creditMemo = CreditMemoPeer::retrieveByPk($request->getParameter('credit_iden'));
    return $this->renderPartial('credit_grid_td',array('creditMemo'=>$creditMemo,));
  }
  

  private function isUnique($credit_iden,$credit_type,$credit_number){
    $c = new Criteria();
    if ($credit_iden)
      $c->addAnd(CreditMemoPeer::CREDIT_IDEN,$credit_iden,Criteria::NOT_EQUAL);
    $c->addAnd(CreditMemoPeer::CREDIT_TYPE,$credit_type);
    $c->addAnd(CreditMemoPeer::CREDIT_NUMBER,$credit_number);
    $c->addJoin(DocumentPeer::DOCUMENT_IDEN,CreditMemoPeer::CREDIT_IDEN);
    $c->addAnd(DocumentPeer::VOID,'N');
    return CreditMemoPeer::doCount($c);
  }
  
  public function executeCheckId(sfWebRequest $request){
    $credit_iden = $request->getParameter('credit_iden');
    $credit_type = $request->getParameter('credit_type');
    $credit_number = $request->getParameter('credit_number');
    $count = $this->isUnique($credit_iden,$credit_type,$credit_number);
    return $this->renderText(json_encode(array('count'=>$count,)));
  }

  
}
